/*
 * Copyright (c) 2018. Beyondsoft Corporation.
 * All Rights Reserved.
 *
 * BEYONDSOFT CONFIDENTIALITY
 *
 * The information in this file is confidential and privileged from Beyondsoft Corporation,
 * which is intended only for use solely by Beyondsoft Corporation.
 * Disclosure, copying, distribution, or use of the contents of this file by persons other than Beyondsoft Corporation
 * is strictly prohibited and may violate applicable laws.
 */
package com.beyondsoft.smarthome.base.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.beyondsoft.smarthome.R;
import com.beyondsoft.smarthome.base.presenter.BaseFragmentPresenter;
import com.beyondsoft.smarthome.utils.LoadingDialog;
import com.beyondsoft.smarthome.utils.MvpInstantiationUtils;
import com.beyondsoft.smarthome.utils.logs.LogTag;

/**
 * @author xzk
 * @data 2019/2/28
 * @email xiezhengkun@beyondsoft.com
 * @remark 基类fragment
 */
public abstract class BaseFragment<T extends BaseFragmentPresenter> extends Fragment {
    protected T presenter;
    protected Context context;
    private Unbinder bind;
    private String title = "";
    private String fragmentTag = "";
    private LoadingDialog loadingDialog;
    //用来保存数据的intent
    protected Intent activityIntent;

    public BaseFragment() {
        String name = getClass().getSimpleName();
        setFragmentTag(name);
        LogTag.d("getClass().getSimpleName()-->"+name);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(), container, false);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bind = ButterKnife.bind(this, view);
        loadingDialog = new LoadingDialog(context);
        presenter = MvpInstantiationUtils.getInstantiation(this, MvpInstantiationUtils.FIRSTGENERICITY);
        if (presenter != null) {
            presenter.bindViewAndModel(this);
        }
        getData(view, savedInstanceState);

        //取消下方点击焦点用 避免点击穿透 到下层view
        this.getView().setOnClickListener(v->{});
    }
    public String getFragmentTag() {
        return fragmentTag;
    }

    public void setFragmentTag(String fragmentTag) {
        this.fragmentTag = fragmentTag;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Context getSupperContext() {
        return context;
    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    public abstract int getLayoutId();

    public abstract void getData(View view, Bundle savedInstanceState);

    public void showLoadingDialog() {
        if (loadingDialog != null && !loadingDialog.isShowing()) {
            loadingDialog.showDialog();
        }
    }

    public void removeLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismissDialog();
        }
    }
    //为关闭刷新保留的方法
    public void closeRefresh(){ }
    @Override
    public void onDestroyView() {
        if(presenter != null){
            presenter.recycleResource();
            presenter = null;
        }
        if(loadingDialog!= null){
            loadingDialog.recycleSource();
            loadingDialog=null;
        }
        if(activityIntent != null){
            activityIntent = null;
        }
        super.onDestroyView();
        bind.unbind();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    public void setActivityIntent(Intent activityIntent) {
        this.activityIntent = activityIntent;
    }
}
